//
//  JFTPopMenu.m
//  JFTBusiness
//
//  Created by 竣付通 on 2017/8/25.
//  Copyright © 2017年 junHaoPay. All rights reserved.
//

#import "JFTPopMenu.h"
#import "configHeader.h"

@interface JFTPopMenu()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,assign)CGFloat cellHeight;
@end
@implementation JFTPopMenu
- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menuBgIcon"]];
         [self addSubview:self.tableView];
    }
    return _tableView;
}
- (void)initTableViewWithData:(NSArray *)dataArray{
    self.cellHeight = 30;
    self.dataArray = [NSMutableArray arrayWithArray:dataArray];
    [self refreshData];
}

#pragma tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [UITableViewCell new];
    }
        UIButton *deleteButton = [[UIButton alloc] initWithFrame:CGRectMake((self.frame.size.width - 10), 0, 40, 40)];
        [deleteButton addTarget:self action:@selector(accessoryButtonIsTapped:event:) forControlEvents:UIControlEventTouchUpInside];
        [deleteButton setImage:[UIImage imageNamed:@"deleteIcon"] forState:UIControlStateNormal];
        [deleteButton setImage:[UIImage imageNamed:@"deleteIcon"] forState:UIControlStateHighlighted];
        cell.accessoryView = deleteButton;
        NSString *str = self.dataArray[indexPath.row];
        cell.textLabel.text = str;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        [cell.textLabel setTextColor:RGB(51, 51, 51)];
        cell.backgroundColor = [UIColor whiteColor];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    id object = self.dataArray[indexPath.row];
    [self.delegate jftPopMenuDidSelectWithObject:object];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.dataArray.count == 0) {
        return 0;
    }else{
    return self.cellHeight;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.cellHeight)];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(5, 0, (self.frame.size.width - 10), 30)];
    btn.backgroundColor = RGB(236, 236, 236);
    [btn setTitle:@"隐藏" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clearBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    return view;
}

- (void)clearBtnDidClick{
    [self.dataArray removeAllObjects];
    [self refreshData];
}

- (void)accessoryButtonIsTapped:(id)sender event:(id)event{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
    if(indexPath != nil)
    {
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [self refreshData];
    }
}

- (void)refreshData{
    if (self.dataArray.count == 0) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 0);
    }else if (self.dataArray.count == 1) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.cellHeight * 2);
    }else if (self.dataArray.count == 2){
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.cellHeight * 3);
    }else{//大于等于3
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.cellHeight * 4);
    }
    self.tableView.frame = self.bounds;
    [self.tableView reloadData];
}
@end
